shell 练习-自动添加项目

需求背景:

服务器上,跑的lamp环境,上面有很多客户的项目,每个项目就是一个网站。 由于客户在不断增加,每次增加一个客户,就需要配置相应的mysql、ftp以及httpd. 这种工作是重复性非常强的,所以用脚本实现非常合适。

mysql增加的是对应客户项目的数据库、用户、密码,ftp增加的是对应项目的用户、密码(使用vsftpd,虚拟用户模式),httpd就是要增加虚拟主机配置段。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/bin/bash
webdir=/home/wwwroot
ftpudir=/etc/vsftpd/vuuser
mysqlc="/usr/bin/mysql -uroot -xxxxxx"
httpd_config_f="/usr/local/apache2/conf/extra/httpd-vhosts.conf"
add_mysql_user()
{
mysql_p=`mkpasswd -s 0 -l 12`
echo "$pro $mysql_p" >/tmp/$pro.txt
$mysqlc <<EOF
grant all on $p.* to "$pro"@'127.0.0.1' identified by "$mysql_p";
EOF
}
add_ftp_user()
{
ftp_p=`mkpasswd -s 0 -l 12`
echo "$pro" >> /root/login.txt
echo "$ftp_p" >> /root/login.txt
db_load -T -t hash -f /root/login.txt /etc/vsftpd/vsftpd_login.db
cd $ftpudir
cp aaa $pro //这里的aaa是一个文件,是之前的一个项目,可以作为配置模板
sed -i "s/aaa/$pro/" $pro //把里面的aaa改为新的项目名字
/etc/init.d/vsftpd restart
}
config_httpd()
{
mkdir $webdir/$pro
chown vsftpd:vsftpd $webdir/$pro
echo -e "<VirtualHost *:80> \n DocumentRoot "/home/internet/www/$pro/" \n ServerName $dom \n #ServerAlias \n</VirtualHost> " >> $httpd_config_f
/usr/local/apache2/bin/apachectl graceful
}
read -p "input the project name: " pro
read -p "input the domain: " dom
add_mysql_user
add_ftp_user
config_httpd